<template>
  <div>
    <el-table
      :data="menuList"
      style="width: 100%"
      row-key="id"
      lazy
      default-expand-all
      ref="table"
      :tree-props="{ children: 'childs' }"
    >
      <el-table-column label="标题" width="200">
        <template slot-scope="scope">
          <div class="el-icon-folder">{{ scope.row.title }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="code" label="权限点代码" width="685">
      </el-table-column>
      <el-table-column prop="address" label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="fixDialogOpen(scope.row)"
            >修改</el-button
          >
          <el-button size="mini" type="danger" @click="open(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <fix-dialog
      :dialogData="dialogData"
      :menuList="menuList"
      @closeDialog="fixDialogClose"
    ></fix-dialog>
  </div>
</template>

<script>
import { menuList, deleteMenu } from '@/api/menu'
import fixDialog from './fix-dialog'
export default {
  data() {
    return {
      menuList: [],
      // 控制修改弹窗的数据
      dialogData: {
        rowInfo: '',
        visible: false
      }
    }
  },
  components: {
    fixDialog
  },
  created() {
    this.getMenuList()
  },
  methods: {
    // 获取列表的数据
    async getMenuList() {
      try {
        const res = await menuList()
        const newRes = JSON.parse(
          JSON.stringify(res.data).replace(/points/g, 'childs')
        )
        this.menuList = newRes
      } catch (err) {}
    },
    fixDialogOpen(row) {
      this.dialogData.rowInfo = row
      this.dialogData.visible = true
    },
    fixDialogClose(close) {
      this.dialogData.visible = close
      this.getMenuList()
    },
    open(row) {
      this.$confirm('此操作将永久删除该菜单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      })
        .then(() => {
          deleteMenu(row.id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getMenuList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      this.getMenuList()
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .el-tree-node__expand-icon {
  display: none;
}
/deep/ .el-icon-arrow-right {
  display: none;
}
/deep/ .el-table__row--level-1 .el-icon-folder::before {
  content: ' \e784';
}
/deep/ .el-table__row--level-2 .el-icon-folder::before {
  content: ' \e6ce';
}
/deep/ .el-table th {
  background-color: #fafafa;
}
</style>
